using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace CourseSync.LibFetcher.Utility
{
    public class StringService
    {
        protected static readonly Regex REGEX_HTML_TAG = new Regex(@"\<[^\>]*\>", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline);
        protected static readonly Regex REGEX_HTML_ESCAPE = new Regex(@"\&[^;]+;", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline);
        protected static readonly Regex REGEX_PHRASE = new Regex(@"[\u4e00-\u9fa5]{2,}", RegexOptions.Compiled | RegexOptions.Multiline);
        protected static readonly Regex REGEX_BR = new Regex(@"<[\s]*br[\s]*[\/]*[\s]*>", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase);
        protected static readonly Regex REGEX_NBSP = new Regex(@"\&nbsp;|\&\#160;", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        protected static readonly Regex REGEX_GT = new Regex(@"\&gt;|\&\#62;", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        protected static readonly Regex REGEX_LT = new Regex(@"\&lt;|\&\#60;", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        protected static readonly Regex REGEX_AMP = new Regex(@"\&amp;|\&\#38;", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        protected static readonly Regex REGEX_QUOT = new Regex(@"\&quot;|\&\#34;", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        protected static readonly Regex REGEX_APOS = new Regex(@"\&apos;|\&\#39;", RegexOptions.IgnoreCase | RegexOptions.Compiled);

        public static string UnescapeJavaScript(string s)
        {
            return s.Replace("\\n", "\n").Replace("\\r", "\r").Replace("\\t", "\t").Replace("\\/", "/")
                .Replace("\\\"", "\"").Replace("\\\'", "\'").Replace("\\\\", "\\");
        }

        public static string UnescapeHtml(string s)
        {
            s = RemoveHtmlTags(REGEX_BR.Replace(s, "\n")).Trim();
            s = REGEX_NBSP.Replace(s, " ");
            s = REGEX_GT.Replace(s, ">");
            s = REGEX_LT.Replace(s, "<");
            s = REGEX_QUOT.Replace(s, "\"");
            s = REGEX_APOS.Replace(s, "\'");
            s = REGEX_AMP.Replace(s, "&");
            return RemoveHtmlEscapes(s);
        }

        public static string RemoveHtmlTags(string s)
        {
            return RemoveHtmlTags(s, " ");
        }

        public static string RemoveHtmlTags(string s, string replacement)
        {
            if (s == null)
                return string.Empty;
            if (replacement == null)
                replacement = string.Empty;
            return REGEX_HTML_TAG.Replace(s, replacement);
        }

        public static string RemoveHtmlEscapes(string s)
        {
            return RemoveHtmlEscapes(s, " ");
        }

        public static string RemoveHtmlEscapes(string s, string replacement)
        {
            if (s == null)
                return string.Empty;
            if (replacement == null)
                replacement = string.Empty;
            return REGEX_HTML_ESCAPE.Replace(s, replacement);
        }

        public static string[] GetPhrases(string s)
        {
            MatchCollection phrase_matches = REGEX_PHRASE.Matches(s);
            List<string> phrases = new List<string>();
            foreach (Match m in phrase_matches)
                phrases.Add(m.Value);
            return phrases.ToArray();
        }

        public static string Join(ICollection<object> array, string seperator)
        {
            if (array == null)
                return null;
            if (array.Count == 0)
                return string.Empty;
            if (seperator == null)
                seperator = string.Empty;
            StringBuilder retval = new StringBuilder();
            foreach (object obj in array)
            {
                retval.Append(seperator);
                if (obj != null)
                    retval.Append(obj.ToString());
            }
            return retval.ToString().Substring(seperator.Length);
        }

    }
}
